package com.itheima.contorller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Employee;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/10/30 11:48
 */

@RequestMapping("/category")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询所有
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R findByPage(int page, int pageSize){
        IPage<Category> iPage = new Page<>(page, pageSize);
        IPage<Category> page1 = categoryService.page(iPage);
        return R.success(page1);
    }

    /**
     * 添加套餐
     * @param category
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category, HttpSession session){
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Long id = (Long) session.getAttribute("employee");
        category.setCreateUser(id);
        category.setUpdateUser(id);
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 修改
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category, HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(id);
        boolean flage = categoryService.updateById(category);
        return flage ? R.success("修改成功"):R.error("修改失败！");
    }

    /**
     *  删除的时候判断是否关联其他表
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteById( Long id){
        Category category = categoryService.getById(id);
        Integer type = category.getType();
        if(type == 1){
            // 菜品分类
            LambdaQueryWrapper<Dish> querryWrapper = new LambdaQueryWrapper<>();
            querryWrapper.eq(Dish::getCategoryId, id);
            int count = dishService.count(querryWrapper);
            if(count > 0){
                return R.error("该菜品分类有菜品，不允许删除！！");
            }
        }else {
            // 套餐分类
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getCategoryId, id);
            int count = setmealService.count(queryWrapper);
            if(count >0){
                return R.error("该菜品分类有菜品，不允许删除！！");
            }

        }
        categoryService.removeById(id);
        return R.success("删除成功！");
    }
}
